package org.micode.demo.db.vo;

import org.micode.common.enums.ResultEnum;
import org.micode.common.exception.DataException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.micode.common.helper.I18nMsgHelper;

@Data
@Slf4j
public class ChangePwdVO {

    private Long modifiedUserId;
    private String loginApp;
    private String loginIp;

    private Long userId;

    private String currentPassword;

    private String newPassword;

    private String confirmPassword;

    public boolean verify() {
        if (null == userId) {
            log.error(I18nMsgHelper.get("valid.user.not-userid"));
            throw new DataException(ResultEnum.UN_LOGIN.getCode(), I18nMsgHelper.get("valid.user.not-login"));
        }

        if (StringUtils.isBlank(currentPassword)) {
            log.error(I18nMsgHelper.get("valid.user.currentPassword.is-null") + ", userId: {}", userId);
            throw new DataException(I18nMsgHelper.get("valid.user.currentPassword.is-null"));
        }

        return verifyNewPwd();
    }

    public boolean verifyNewPwd() {
        if (StringUtils.isBlank(newPassword)) {
            log.error(I18nMsgHelper.get("valid.user.newPassword.is-null") + ", userId: {}", userId);
            throw new DataException(I18nMsgHelper.get("valid.user.newPassword.is-null"));
        }

        if (StringUtils.isBlank(confirmPassword)) {
            log.error(I18nMsgHelper.get("valid.user.confirmPassword.is-null") + ", userId: {}", userId);
            throw new DataException(I18nMsgHelper.get("valid.user.confirmPassword.is-null"));
        }

        if (!confirmPassword.equals(newPassword)) {
            log.error(I18nMsgHelper.get("valid.user.password.inconsistent") + ", userId: {}", userId);
            throw new DataException(I18nMsgHelper.get("valid.user.password.inconsistent"));
        }
        return true;
    }

    public String getUpdUser() {
        return null == modifiedUserId ? "" : modifiedUserId.toString();
    }

    public String getUpdProg() {
        return null == loginApp ? "" : loginApp;
    }

    public String getUpdIp() {
        return null == loginIp ? "" : loginIp;
    }

}
